/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.mail.repository.service;

import cn.hutool.core.bean.BeanUtil;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import info.peigen.hotpot.component.mail.domain.MailTemplate;
import info.peigen.hotpot.component.mail.manage.service.MailTemplateManageConvertor;
import info.peigen.hotpot.component.mail.repository.entity.MailTemplateEntity;
import info.peigen.hotpot.component.mail.repository.mapper.MailTemplateMapper;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldDefaults;
import org.noear.solon.annotation.Component;

/**
 * <b>(MailTemplateRepository)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/1/22
 */
@Data
@EqualsAndHashCode(callSuper = true)
@FieldDefaults(level = AccessLevel.PRIVATE)
@Component
public class MailTemplateRepository extends AbstractBeetlSqlRepository<MailTemplateMapper, MailTemplateEntity> {
    public void store(MailTemplate mailTemplate) {
        MailTemplateEntity entity = BeanUtil.copyProperties(mailTemplate, MailTemplateEntity.class, "context");
        entity.setContext(mailTemplate.getContext());
        saveOrUpdate(entity);
    }

    public MailTemplate active(String templateId) {
        MailTemplateEntity entity = getMapper().findByTemplateId(templateId);
        return MailTemplateManageConvertor.convertToDomain(entity);
    }

    public void deleteByTemplateId(String templateId) {
        getMapper().deleteByTemplateId(templateId);
    }
}